package com.example.demo.utils;


import org.springframework.util.StringUtils;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 */
public class StringUtil
{

    /**
     * 判断Object类型 为空
     *
     * @param object
     * @return
     */
    public static boolean isNull(Object object)
    {
        if (object instanceof String)
        {
            return isEmpty(object.toString());
        }
        return (object == null);
    }

    /**
     * 判断String类型 为空
     *
     * @param value
     * @return
     */
    public static boolean isEmpty(String value)
    {
        return ((value == null) || (value.trim().length() == 0) || ("null".endsWith(value)));
    }

    /**
     * Object类型为空时转换为 ""
     *
     * @param obj
     * @return
     */
    public static String null2String(Object obj)
    {
        return ((obj == null) ? "" : obj.toString());
    }

    /**
     * String类型为空时转换为 ""
     *
     * @param str
     * @return
     */
    public static String null2String(String str)
    {
        return ((str == null) ? "" : str);
    }

    /**
     * 格式化钱
     *
     * @param currency
     * @return
     */
    public static String formatCurrecy(String currency)
    {
        if (isEmpty(currency))
        {
            return "";
        }

        NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
        try
        {
            return usFormat.format(Double.parseDouble(currency));
        } catch (Exception e)
        {
        }
        return "";
    }

    public static String iso2Gb(String gbString)
    {
        if (gbString == null)
        {
            return null;
        }
        String outString = "";
        try
        {
            byte[] temp = null;
            temp = gbString.getBytes("ISO8859-1");
            outString = new String(temp, "GB2312");
        } catch (Exception e)
        {
        }
        return outString;
    }

    public static String iso2Utf(String isoString)
    {
        if (isoString == null)
        {
            return null;
        }
        String outString = "";
        try
        {
            byte[] temp = null;
            temp = isoString.getBytes("ISO8859-1");
            outString = new String(temp, "UTF-8");
        } catch (Exception e)
        {
        }
        return outString;
    }

    public static String str2Gb(String inString)
    {
        if (inString == null)
        {
            return null;
        }
        String outString = "";
        try
        {
            byte[] temp = null;
            temp = inString.getBytes();
            outString = new String(temp, "GB2312");
        } catch (Exception e)
        {
        }
        return outString;
    }

    public static String replace(String str, String[] vals)
    {
        if (!StringUtils.isEmpty(str) && vals != null && vals.length > 0)
        {
            for (int i = 0; i < vals.length; i++)
            {
                str = str.replace("{" + (i + 1) + "}", vals[i]);
            }
        }

        return str;
    }

    /**
     * 过滤空对象
     *
     * @param obj
     * @return
     */
    public static String parseString(Object obj)
    {
        boolean flag = false;
        if (isNull(obj))
        {
            flag = true;
        }
        if (flag == false)
        {
            if ("null".equalsIgnoreCase(obj.toString()) || "".equalsIgnoreCase(obj.toString()))
            {
                flag = true;
            }
        }
        if (flag)
        {
            return "";
        } else
        {
            return obj.toString().trim();
        }
    }

    /**
     * 过滤回车符
     *
     * @param value
     * @return
     */
    public static String filterEnter(String value)
    {
        value = parseString(value);
        value = value.replaceAll("\r", "");
        value = value.replaceAll("\n", "<br/>");
        return value;
    }

    /**
     * 过滤掉SQL中包含 1 = 1 部分内容
     *
     * @param sql
     * @return
     */
    public static String filterSQL(String sql)
    {
        Pattern pattern1 = Pattern.compile("[\\s]*(.+)[\\s]*=[\\s]*\\1[\\s]+((and)|(or))[\\s]+", Pattern.CASE_INSENSITIVE);
        Pattern pattern2 = Pattern.compile("[\\s]*((and)|(or))[\\s]+(.+)[\\s]*=[\\s]*\\4[\\)]", Pattern.CASE_INSENSITIVE);
        Pattern pattern3 = Pattern.compile("[\\s]*((and)|(or))[\\s]+(.+)[\\s]*=[\\s]*\\4[\\s]+", Pattern.CASE_INSENSITIVE);
        Pattern pattern4 = Pattern.compile("[\\s]*where[\\s]+(.*)[\\s]*=[\\s]*\\1[\\)]", Pattern.CASE_INSENSITIVE);
        Pattern pattern5 = Pattern.compile("[\\s]*where[\\s]+(.*)[\\s]*=[\\s]*\\1[\\s]+", Pattern.CASE_INSENSITIVE);

        String str = sql;
        str = " " + str;
        str = str + " ";

        Matcher matcher = pattern1.matcher(str);
        while (matcher.find())
        {
            str = matcher.replaceAll(" ");
        }
        matcher = pattern2.matcher(str);
        while (matcher.find())
        {
            str = matcher.replaceAll(" ) ");
        }
        matcher = pattern3.matcher(str);
        while (matcher.find())
        {
            str = matcher.replaceAll(" ");
        }
        matcher = pattern4.matcher(str);
        while (matcher.find())
        {
            str = matcher.replaceAll(" ) ");
        }
        matcher = pattern5.matcher(str);
        while (matcher.find())
        {
            str = matcher.replaceAll(" ");
        }
        return str.trim();
    }

    /**
     * 判断日期
     *
     * @param date
     * @return
     */
    public static boolean checkDate(String date)
    {
        String eL = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
        Pattern p = Pattern.compile(eL, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(date);
        boolean b = m.matches();
        return b;
    }

    public static String getRandomNum()
    {
        String t = String.valueOf(System.currentTimeMillis());
        t = t.substring(t.length() - 5, t.length());
        String rad = "0123456789";
        StringBuffer result = new StringBuffer();
        java.util.Random rand = new java.util.Random();
        int length = 27;
        for (int i = 0; i < length; i++)
        {
            int randNum = rand.nextInt(10);
            result.append(rad.substring(randNum, randNum + 1));
        }
        return t + result;
    }

    /**
     * 按长度获取随机数
     *
     * @param length
     * @return
     */
    public static String getRandomNumByLength(int length)
    {
        String rad = "0123456789";
        java.util.Random rand = new java.util.Random();
        StringBuffer result = new StringBuffer();
        if (length > 0)
        {
            for (int i = 0; i < length; i++)
            {
                int randNum = rand.nextInt(10);
                result.append(rad.substring(randNum, randNum + 1));
            }
        }
        return result.toString();
    }

    /**
     * 首字母转小写
     *
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s)
    {
        if (Character.isLowerCase(s.charAt(0)))
        {
            return s;
        } else
        {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 首字母转大写
     *
     * @param s
     * @return
     */
    public static String toUpperCaseFirstOne(String s)
    {
        if (Character.isUpperCase(s.charAt(0)))
        {
            return s;
        } else
        {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 判断是否是中文
     *
     * @param str
     * @return
     */
    public static boolean isContainChinese(String str)
    {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find())
        {
            return true;
        }
        return false;
    }

    public static String fillLeft(String paramString, char paramChar, int paramInt)
    {
        return fillStr(paramString, paramChar, paramInt, true);
    }

    public static String fillRight(String paramString, char paramChar, int paramInt)
    {
        return fillStr(paramString, paramChar, paramInt, false);
    }

    private static String fillStr(String paramString, char paramChar, int paramInt, boolean paramBoolean)
    {
        int i = paramInt - paramString.length();

        if (i <= 0)
        {
            return paramString;
        }
        StringBuilder localStringBuilder = new StringBuilder(paramString);
        for (; i > 0; --i)
        {
            if (paramBoolean)
            {
                localStringBuilder.insert(0, paramChar);
            } else
            {
                localStringBuilder.append(paramChar);
            }
        }
        return localStringBuilder.toString();
    }

}